reading from file

Reading Strings from file

 

To retrieve the contents of a text file, open the file for sequential Input. Then use the Line Input #, Input( ), or Input # statement to copy the file into program variables.
Visual Basic provides statements and functions that will read and write sequential files one character at a time or one line at a time.
For example, the following code fragment reads a file line by line:


Dim LinesFromFile, NextLine As String

Do Until EOF(FileNum)
   Line Input #FileNum, NextLine
   LinesFromFile = LinesFromFile + NextLine + Chr(13) + Chr(10)
Loop

Although Line Input # recognizes the end of a line when it comes to the carriage return–linefeed sequence, it does not include the carriage return–linefeed when it reads the line into the variable. If you want to retain the carriage return–linefeed, your code must add it.
You can also use the Input # statement, which reads a list of numbers and/or string expressions written to the file. For example, to read in a line from a mailing list file, you might use the following statement:


Input #FileNum, name, street, city, state, zip

You can use the Input function to copy any number of characters from a file to a variable, provided the variable is large enough. For example, the following code uses Input to copy the specified number of characters to a variable:


LinesFromFile = Input(n, FileNum)

To copy an entire file to a variable, use the InputB function to copy bytes from a file to a variable. Since the InputB function returns an ANSI string, you must use the StrConv function to convert the ANSI string to a UNICODE string as follows:


LinesFromFile = StrConv(InputB(LOF(FileNum), FileNum), vbUnicode)

 

 

basic file handling by v. vanthana